package com.doroodo.sys.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.doroodo.base.action.BaseAction;
import com.doroodo.config.SysVal;
import com.doroodo.base.model.Tree;
import com.doroodo.base.vo.web.LeftMenu;
import com.doroodo.base.vo.web.Web;
import com.doroodo.base.vo.web.LeftMenu.Menu;
import com.doroodo.base.vo.web.LeftMenu.Menu.Menus;
import com.doroodo.sys.model.SyModule;
import com.doroodo.sys.model.SyParameter;
import com.doroodo.sys.model.SyParameterUsed;
import com.doroodo.sys.model.SyRole;
import com.doroodo.sys.model.SyUser;
import com.doroodo.sys.service.SyModuleService;
import com.doroodo.sys.service.SyParameterService;
import com.doroodo.sys.service.SyRoleService;
import com.doroodo.sys.service.SyUserService;
@Controller
@Results({
	@Result(name="tsbStateData", location="/work/TsbStateData.jsp"),
	@Result(name="tsbEq", location="/work/TsbEq.jsp"),
	@Result(name="tsbTest", location="/work/TsbTest.jsp"),
	@Result(name="zzz", location="/work/Zzz.jsp"),
	@Result(name="hhh", location="/work/Hhh.jsp"),
	@Result(name="ggg", location="/work/Ggg.jsp"),
	@Result(name="ttt", location="/work/Ttt.jsp"),
	@Result(name="yyy", location="/work/Yyy.jsp"),
	@Result(name="xxx", location="/work/Xxx.jsp"),
	@Result(name="istest", location="/work/Istest.jsp"),
	@Result(name="syDemoTest", location="/work/SyDemoTest.jsp"),
	@Result(name="syTest_m", location="/App/work/SyTest.html"),
	//系统路由，请勿删除   start
	@Result(name="syProject", location="/sys/SyProject.jsp"),
	@Result(name="syDemoTreeiframe", location="/sys/SyDemoTreeiframe_frame.jsp"),
	@Result(name="syDemoDatetree", location="/sys/SyDemoDatetree.jsp"),
	@Result(name="syDemoGridtochart", location="/sys/SyDemoGridtochart.jsp"),
	@Result(name="syDebug", location="/sys/SyDebug.jsp"),
	@Result(name="syDebugState", location="/sys/SyDebugState.jsp"),
	@Result(name="syChart", location="/sys/SyChart.jsp"),
	@Result(name="syNotice", location="/sys/SyNotice.jsp"),
	@Result(name="syAssembly", location="/sys/SyAssembly.jsp"),
	@Result(name="syWebmodal", location="/sys/SyWebmodal.jsp"),
	@Result(name="syLog", location="/sys/SyLog.jsp"),
	@Result(name="syFile", location="/sys/SyFile.jsp"),
	@Result(name="syFormDesign", location="/sys/SyFormDesign.jsp"),
	@Result(name="syModule", location="/sys/SyModule.jsp"),
	@Result(name="syUser", location="/sys/SyUser_frame.jsp"),
	@Result(name="syOrgan", location="/sys/SyOrgan_frame.jsp"),
	@Result(name="syRole", location="/sys/SyRole.jsp"),
	@Result(name="syDesign", location="/sys/SyDesign_frame.jsp"),
	@Result(name="syParameter", location="/sys/SyParameter.jsp"),
	@Result(name="syFlow", location="/sys/SyFlow.jsp"),
	//手机路由
	@Result(name="syDemoTreeiframe_m", location="/ME/sys/SyDemoTreeiframe_frame.jsp"),
	@Result(name="syDemoDatetree_m", location="/ME/sys/SyDemoDatetree.jsp"),
	@Result(name="syDemoGridtochart_m", location="/ME/sys/SyDemoGridtochart.jsp"),
	@Result(name="syDebug_m", location="/ME/sys/SyDebug.jsp"),
	@Result(name="syDebugState_m", location="/ME/sys/SyDebugState.jsp"),
	@Result(name="syChart_m", location="/ME/sys/SyChart.jsp"),
	@Result(name="syNotice_m", location="/ME/sys/SyNotice.jsp"),
	@Result(name="syAssembly_m", location="/ME/sys/SyAssembly.jsp"),
	@Result(name="syWebmodal_m", location="/ME/sys/SyWebmodal.jsp"),
	@Result(name="syLog_m", location="/ME/sys/SyLog.jsp"),
	@Result(name="syFile_m", location="/ME/sys/SyFile.jsp"),
	@Result(name="syFormDesign_m", location="/ME/sys/SyFormDesign.jsp"),
	@Result(name="syModule_m", location="/ME/sys/SyModule.jsp"),
	@Result(name="syUser_m", location="/ME/sys/SyUser_frame.jsp"),
	@Result(name="syOrgan_m", location="/ME/sys/SyOrgan_frame.jsp"),
	@Result(name="syRole_m", location="/ME/sys/SyRole.jsp"),
	@Result(name="syDesign_m", location="/ME/sys/SyDesign_frame.jsp"),
	@Result(name="syParameter_m", location="/ME/sys/SyParameter.jsp"),
	@Result(name="syFlow_m", location="/ME/sys/SyFlow.jsp")
	//系统路由，请勿删除   end
})  
@ParentPackage(value="sys")   
@InterceptorRef("mydefault")  
public class SyParameterAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	@Autowired
	private SyUserService syUserService;
	@Autowired
	private SyParameterService syParameterService;
	@Autowired
	private SyModuleService syModuleService;
	@Autowired
	private SyRoleService syRoleService;
	private SyParameter syParameter;
	private Map visibleMap=null;
	
	@Action("/sys/syParameter_Add")
	public void syParameterAdd(){
		Map<String, String> map = new HashMap<String, String>();
		syParameterService.saveOrUpdate(syParameter);
		writeMsg(SysVal.ADD_SUC);
	}
	
	private Map isVisible(List<SyModule> syModules){
		String userRole=this.getLoginUserRole();  
		Map visibleMap = new HashMap();
		 if(userRole!=null){  
			 SyRole syRole = syRoleService.getBySyRoleName(userRole);
			 if((syRole!=null) && (!syRole.getRolename().equalsIgnoreCase("超级管理员"))){
				 String operatemapStr = syRole.getOperatemap();
				 String[] operatemapStrs = operatemapStr.split(",");
				 for(int i=0;i<operatemapStrs.length;i++){
					 String[] operatemaps = operatemapStrs[i].split(":");
					 if(operatemaps.length>=2){
						 visibleMap.put(operatemaps[0], operatemaps[1]);
					 }
				 }
			 }else{
				for(int i=0;i<syModules.size();i++){
					SyModule syModule=syModules.get(i);
					visibleMap.put(syModule.getMenuid(), "1");
				}
			 }
		 }
		 return visibleMap;
	}
	
	@Action("/sys/syParameter_Get_MainWebInfo")
	public void syParameterGetMainWebInfo(){
		Web web = new Web();
		SyParameter syParameter=(SyParameter) syParameterService.getUsedSyParameter();
		SyUser syUser=syUserService.getByUserId(this.getLoginUserId());
		web.setC_m_footer(syParameter.getFooter());
		web.setC_m_navtitle(syParameter.getNavtitle());
		web.setI_u_userip(getRequest().getRemoteAddr());
		web.setI_u_username(syUser.getUsername());
		web.setI_u_userrole(syUser.getRolename());
		web.setI_u_userid(syUser.getUserid());
		web.setTitle(syParameter.getTitle());
		web.setLeftMenu(leftMenu());
		web.setThemename(syUser.getThemename());
		this.writeJson(web);
	}
	
	@Action("/sys/syParameter_Get_LoginWebInfo")
	public void syParameterGetLoginWebInfo(){
		Web web = new Web();
		SyParameter syParameter=null;
		try{
			syParameter=(SyParameter) syParameterService.getUsedSyParameter();
		}catch(Exception e){
			e.printStackTrace();
		}
		web.setI_u_login_foot(syParameter.getFooter());
		web.setI_u_login_header(syParameter.getTitle());
		this.writeJson(web);
	}
	
	@Action("/sys/syParameter_Get_WebLeftMenuInfo")
	public void syParameterGetWebLeftMenuInfo(){
		this.writeJson(toTree());
	}
	
	private List<Tree> toTree(){
		List<Tree> ts =new ArrayList();
		List<Menu> ms=leftMenu().getMenus();
		for(int i=0;i<ms.size();i++){
			Tree t = new Tree();
			t.setId(ms.get(i).getMenuid());
			t.setText(ms.get(i).getMenuname());
			List<Menus> mss = ms.get(i).getMenus();
			List<Tree> cs = new ArrayList();
			for(int j=0;j<mss.size();j++){
				Tree mt=new Tree();
				mt.setId(mss.get(j).getMenuid());
				mt.setText(mss.get(j).getMenuname());
				mt.setState("open");
				cs.add(mt);
			}
			t.setChildren(cs);
			t.setState("open");
			ts.add(t);
		}
		return ts;
	}
	
	public LeftMenu leftMenu(){
		String pid="i_m_leftmenu";
		LeftMenu leftMenu = new LeftMenu(pid);
		List<Menu> menusll=new ArrayList();
		menusll.addAll(getModules(leftMenu));
		leftMenu.setMenus(menusll);
		return leftMenu;
	}
	
	private Comparator<Menu> getMenuCompBySort(){
		Comparator<Menu> comparator = new Comparator<Menu>(){
			   public int compare(Menu s1, Menu s2) {
				   return s1.getSort()-s2.getSort();
			   }};
			   return comparator;
	}
	
	private Comparator<Menu.Menus> getMenusCompBySort(){
		Comparator<Menu.Menus> comparator = new Comparator<Menu.Menus>(){
			   public int compare(Menu.Menus s1, Menu.Menus s2) {
				   return s1.getSort()-s2.getSort();
			   }};
			   return comparator;
	}
	
	public List<Menu> getModules(LeftMenu leftMenu){
		List<Menu> menusll=new ArrayList();
		List<SyModule> syModules=syModuleService.list();
		if(visibleMap==null){visibleMap=isVisible(syModules);}
		for(int i=0;i<syModules.size();i++){
			SyModule syModule =syModules.get(i);
			if(syModule.getPid().equalsIgnoreCase("0")){//menu
				//syModules.remove(i); //优化
				Menu menu=leftMenu.new Menu(syModule.getMenuid(),syModule.getIcon(),syModule.getMenuname(),syModule.getSort());
				List<Menus> menusl=new ArrayList();
				for(int j=0;j<syModules.size();j++){
					SyModule syModule_ =syModules.get(j);
					if(!(visibleMap.get(syModule_.getMenuid())==null?"":visibleMap.get(syModule_.getMenuid())).toString().split("-")[0].equalsIgnoreCase("1")){
						continue;
					}
					if(syModule_.getPid().equalsIgnoreCase(syModule.getMenuid())){
						//syModules.remove(j); //优化
						Menus menus=null;
						if(syModule_.getUrl().indexOf("http://")==0){
							menus=menu.new Menus(syModule_.getMenuid(),syModule_.getIcon(),syModule_.getMenuname(),syModule_.getUrl(),syModule_.getSort());
						}else{
							menus=menu.new Menus(syModule_.getMenuid(),syModule_.getIcon(),syModule_.getMenuname(),getRequest().getContextPath()+syModule_.getUrl(),syModule_.getSort());
						}
						menusl.add(menus);
					}
				}
				if(menusl.size()!=0){
					Collections.sort(menusl,getMenusCompBySort());
					menu.setMenus(menusl);
					menusll.add(menu);
				}
			}
		}
		Collections.sort(menusll,getMenuCompBySort());
		return menusll;
	}
	
	@Action("/sys/syParameter_List")
	public void syParameterList(){
		this.writeJson(syParameterService.dataGrid(this.getPage(), this.getRows(), this.getSearchName(), this.getSearchKey()));
	}
	
	@Action("/sys/syParameter_Excel")
	public void syParameterExcel(){
		 String title = "Excel表";
		 List<Object> list = syParameterService.dataGrid(this.getPage(), this.getRows(), this.getSearchName(), this.getSearchKey()).getRows();//获取数据
		 super.excel(title,list);
	}
	
	@Action("/sys/syParameter_Set_Defult")
	public void syParameterSetDefult(){
		if(this.getIds().trim()=="")return;
		SyParameterUsed syParameterUsed = new SyParameterUsed();
		syParameterUsed.setId(1);
		String[] ids_=this.getIds().split(",");
		if(ids_.length!=1) {writeMsg(SysVal.SET_ER); return;}
		syParameterUsed.setSyParameterId(this.getIds());
		syParameterService.setDefult(syParameterUsed);
		writeMsg(SysVal.SET_SUC);
	}
	
	@Action("/sys/syParameter_Delete")
	public void syParameterDelete(){
		if(this.getIds().trim()=="")return;
		SyParameter syParameter=(SyParameter) syParameterService.getUsedSyParameter();
		if(syParameter!=null) {
			String[] ids_=this.getIds().split(",");
			for(int i=0;i<ids_.length;i++)
			{
				if(ids_[i].equalsIgnoreCase(String.valueOf(syParameter.getId()))){
					this.writeMsg(SysVal.DEL_ER_MAIN);
					return ;
				}
			}
		}
		syParameterService.delete(this.getIds());
		writeMsg(SysVal.DEL_SUC);
	}
	
	@Action("/sys/syParameter_Update")
	public void syParameterUpdate(){
		if(syParameter!=null) syParameterService.saveOrUpdate(syParameter);
		writeMsg(SysVal.EDIT_SUC);
	}
	public SyParameter getSyParameter() {
		return syParameter;
	}
	public void setSyParameter(SyParameter syParameter) {
		this.syParameter = syParameter;
	}
	//系统路由，请勿删除   start
	@Action("/sys/syParameter_Go_SyParameter") 
	public String syParameterGoSyParameter(){return "syParameter";}
	@Action("/sys/syParameter_Go_SyModule")  
	public String syParameterGoSyModule(){return "syModule";}
	@Action("/sys/syParameter_Go_SyFlow")  
	public String syParameterGoSyFlow(){return "syFlow";}
	@Action("/sys/syParameter_Go_SyUser") 
	public String syParameterGoSyUser(){return "syUser";}
	@Action("/sys/syParameter_Go_SyOrgan") 
	public String syParameterGoSyOrgan(){return "syOrgan";}
	@Action("/sys/syParameter_Go_SyRole") 
	public String syParameterGoSyRole(){return "syRole";}
	@Action("/sys/syParameter_Go_SyDesign") 
	public String syParameterGoSyDesign(){return "syDesign";}
	@Action("/sys/syParameter_Go_SyFile")
	public String syParameterGoSyFile(){return "syFile";}
	@Action("/sys/syParameter_Go_syFormDesign")
	public String syParameterGosyFormDesign(){return "syFormDesign";}
	@Action("/sys/syParameter_Go_SyLog")
	public String syParameterGoSyLog(){return "syLog";}
	@Action("/sys/syParameter_Go_SyAssembly")
	public String syParameterGoSyAssembly(){return "syAssembly";}
	@Action("/sys/syParameter_Go_SyNotice")
	public String syParameterGoSyNotice(){return "syNotice";}
	@Action("/sys/syParameter_Go_SyChart")
	public String syParameterGoSyChart(){return "syChart";}
	@Action("/sys/syParameter_Go_SyDebugState")
	public String syParameterGoSyDebugState(){return "syDebugState";}
	@Action("/sys/syParameter_Go_SyDebug")
	public String syParameterGoSyDebug(){return "syDebug";}
	@Action("/sys/syParameter_Go_SyWebmodal")
	public String syParameterGoSyWebmodal(){return "syWebmodal";}
	@Action("/sys/syParameter_Go_SyDemoGridtochart")
	public String syParameterGoSyDemoGridtochart(){return "syDemoGridtochart";}
	@Action("/sys/syParameter_Go_SyDemoDatetree")
	public String syParameterGoSyDemoDatetree(){return "syDemoDatetree";}
	@Action("/sys/syParameter_Go_SyDemoTreeiframe")
	public String syParameterGoSyDemoTreeiframe(){return "syDemoTreeiframe";}
	@Action("/sys/syParameter_Go_SyProject")
	public String syParameterGoSyProject(){return "syProject";}
	//手机路由
	@Action("/sys/syParameter_Go_SyParameter_m") 
	public String syParameterGoSyParameter_m(){return "syParameter_m";}
	@Action("/sys/syParameter_Go_SyModule_m")  
	public String syParameterGoSyModule_m(){return "syModule_m";}
	@Action("/sys/syParameter_Go_SyFlow_m")  
	public String syParameterGoSyFlow_m(){return "syFlow_m";}
	@Action("/sys/syParameter_Go_SyUser_m") 
	public String syParameterGoSyUser_m(){return "syUser_m";}
	@Action("/sys/syParameter_Go_SyOrgan_m") 
	public String syParameterGoSyOrgan_m(){return "syOrgan_m";}
	@Action("/sys/syParameter_Go_SyRole_m") 
	public String syParameterGoSyRole_m(){return "syRole_m";}
	@Action("/sys/syParameter_Go_SyDesign_m") 
	public String syParameterGoSyDesign_m(){return "syDesign_m";}
	@Action("/sys/syParameter_Go_SyFile_m")
	public String syParameterGoSyFile_m(){return "syFile_m";}
	@Action("/sys/syParameter_Go_syFormDesign_m")
	public String syParameterGosyFormDesign_m(){return "syFormDesign_m";}
	@Action("/sys/syParameter_Go_SyLog_m")
	public String syParameterGoSyLog_m(){return "syLog_m";}
	@Action("/sys/syParameter_Go_SyAssembly_m")
	public String syParameterGoSyAssembly_m(){return "syAssembly_m";}
	@Action("/sys/syParameter_Go_SyNotice_m")
	public String syParameterGoSyNotice_m(){return "syNotice_m";}
	@Action("/sys/syParameter_Go_SyChart_m")
	public String syParameterGoSyChart_m(){return "syChart_m";}
	@Action("/sys/syParameter_Go_SyDebugState_m")
	public String syParameterGoSyDebugState_m(){return "syDebugState_m";}
	@Action("/sys/syParameter_Go_SyDebug_m")
	public String syParameterGoSyDebug_m(){return "syDebug_m";}
	@Action("/sys/syParameter_Go_SyWebmodal_m")
	public String syParameterGoSyWebmodal_m(){return "syWebmodal_m";}
	@Action("/sys/syParameter_Go_SyDemoGridtochart_m")
	public String syParameterGoSyDemoGridtochart_m(){return "syDemoGridtochart_m";}
	@Action("/sys/syParameter_Go_SyDemoDatetree_m")
	public String syParameterGoSyDemoDatetree_m(){return "syDemoDatetree_m";}
	@Action("/sys/syParameter_Go_SyDemoTreeiframe_m")
	public String syParameterGoSyDemoTreeiframe_m(){return "syDemoTreeiframe_m";}
	//系统路由，请勿删除   end
	@Action("/sys/syParameter_Go_SyTest_m")
	public String syParameterGoSyTest_m(){return "syTest_m";}
	@Action("/sys/syParameter_Go_SyDemoTest")
	public String syParameterGoSyDemoTest(){return "syDemoTest";}
	@Action("/sys/syParameter_Go_Istest")
	public String syParameterGoIstest(){return "istest";}
	@Action("/sys/syParameter_Go_Xxx")
	public String syParameterGoXxx(){return "xxx";}
	@Action("/sys/syParameter_Go_Yyy")
	public String syParameterGoYyy(){return "yyy";}
	@Action("/sys/syParameter_Go_Ttt")
	public String syParameterGoTtt(){return "ttt";}
	@Action("/sys/syParameter_Go_Ggg")
	public String syParameterGoGgg(){return "ggg";}
	@Action("/sys/syParameter_Go_Hhh")
	public String syParameterGoHhh(){return "hhh";}
	@Action("/sys/syParameter_Go_Zzz")
	public String syParameterGoZzz(){return "zzz";}
	@Action("/sys/syParameter_Go_TsbTest")
	public String syParameterGoTsbTest(){return "tsbTest";}
	@Action("/sys/syParameter_Go_TsbEq")
	public String syParameterGoTsbEq(){return "tsbEq";}
	@Action("/sys/syParameter_Go_TsbStateData")
	public String syParameterGoTsbStateData(){return "tsbStateData";}
}
